<template>

    <!-- 添加或修改对话框 -->
    <lam-dialog :title="title" :visible.sync="open" width="680px" append-to-body @close="cancel">
      <el-form :ref="formRef" :model="form" :rules="rules" label-width="100px" :disabled="formDisabled">
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" prop="areaName">
              <el-input v-model="form.areaName" clearable placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="父地区" prop="parentId">
              <treeselect v-model="form.parentId" :options="treeData" :show-count="true" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <lam-dict-select type="radio" v-model="form.status" dictCode="status_type" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" autosize placeholder="" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :loading="loading">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </lam-dialog>

</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {httpGet} from '@/api';
import { EditMixin } from '@/components/mixins/EditMixin';

export default {
  name: "EditArea",
  mixins:[EditMixin],
  components:{
    Treeselect
  },
  data() {
    return {
      url: {
        tree: '/mall/area/treeselect',
        base: '/mall/area/'
      },
      // 表单校验
      rules: {
        areaName: [{required: true, message: "名称不能为空", trigger: "blur" }, {validator: this.areaNameUniqueValidator, trigger: 'blur'}],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      },
      treeData:[],
    };
  },
  created() {
  },
  methods: {
    // 表单重置
    reset() {
      this.form = {
        areaName: null,
        parentId: null,
        status: 1,
        remark: null
      };
    },

    /** 查询下拉树列表 */
    loadTreeData(neId){
      httpGet(this.url.tree, {neId: neId}).then(res => {
        this.treeData = res.data;
      });
    },

    beforeOpen(title, operType, res){
      this.loadTreeData(res.data.id);
      res.data.parentId = res.data.parentId==0?null:res.data.parentId;
    },

    areaNameUniqueValidator(rule, value, callback) {
      let param = {
        areaName: value,
        parentId: this.form.parentId==null?0:this.form.parentId
      };
      if(this.operType=='update') param.id = this.form.id;
      this.checkUniqueAsyn(value, callback, 'areaName', param);
    },

    /** 提交按钮 */
    submitForm(){
      this.saveForm(this.url.base);
    }

  }
};
</script>
